package com.zzyl.serve.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zzyl.serve.domain.DeviceData;
import com.zzyl.serve.vo.DeviceDataVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 设备数据Mapper接口
 * 
 * @author liuyp
 * @date 2025-08-23
 */
@Mapper
public interface DeviceDataMapper extends BaseMapper<DeviceData>
{
    /**
     * 查询设备数据
     * 
     * @param id 设备数据主键
     * @return 设备数据
     */
    public DeviceData selectDeviceDataById(Long id);

    /**
     * 查询设备数据列表
     * 
     * @param deviceData 设备数据
     * @return 设备数据集合
     */
    public List<DeviceData> selectDeviceDataList(DeviceData deviceData);

    /**
     * 新增设备数据
     * 
     * @param deviceData 设备数据
     * @return 结果
     */
    public int insertDeviceData(DeviceData deviceData);

    /**
     * 修改设备数据
     * 
     * @param deviceData 设备数据
     * @return 结果
     */
    public int updateDeviceData(DeviceData deviceData);

    /**
     * 删除设备数据
     * 
     * @param id 设备数据主键
     * @return 结果
     */
    public int deleteDeviceDataById(Long id);

    /**
     * 批量删除设备数据
     * 
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteDeviceDataByIds(Long[] ids);


    /*
    /*
    按天的查询，日期取 时+分
    * */
    @Select("SELECT DATE_FORMAT(alarm_time, '%H:00') AS dateTime, FORMAT(ROUND(data_value, 0), 1) AS dataValue " +
            "FROM device_data " +
            "WHERE iot_id = #{iotId} " +
            "AND function_id = #{functionId} " +
            "AND alarm_time BETWEEN #{start} AND #{end} " +
            "ORDER BY alarm_time")
    @Results({
            @Result(column = "dateTime", property = "dateTime"),
            @Result(column = "dataValue", property = "dataValue")
    })
    List<DeviceDataVO> queryDeviceDataListByDay(
            @Param("iotId") String iotId,
            @Param("functionId") String functionId,
            @Param("start") String start,
            @Param("end") String end
    );

    /*
    按周的查询，日期取 月+日
    */
    @Select("SELECT  " +
            "    DATE_FORMAT(alarm_time, '%m.%d') AS dateTime, " +
            "    FORMAT(ROUND(AVG(data_value), 0), 1) AS dataValue " +
            "FROM device_data " +
            "WHERE iot_id = #{iotId} " +
            "  AND function_id = #{functionId} " +
            "  AND alarm_time BETWEEN #{start} AND #{end} " +
            "GROUP BY DATE(alarm_time) " +
            "ORDER BY alarm_time; "
    )
    @Results({
            @Result(column = "dateTime", property = "dateTime"),
            @Result(column = "dataValue", property = "dataValue")})
    List<DeviceDataVO> queryDeviceDataListByWeek(
            @Param("iotId") String iotId,
            @Param("functionId") String functionId,
            @Param("start") String start,
            @Param("end") String end);
}
